﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.IO;

namespace AAA.Base.Util.Json
{
    public class JsonUtil
    {
		public static string Serialize(object value)
		{
			return Serialize(value, true);
		}

        public static string Serialize(object value,bool formated)
        {
			if(formated) 
				return JsonConvert.SerializeObject(value, Formatting.Indented);
            else
				return JsonConvert.SerializeObject(value);
        }

        public static T Deserialize<T>(string strValue)
        {
            return (T)JsonConvert.DeserializeObject<T>(strValue);
        }

		public static T DeserializeFromFile<T>(string strFilename)
		{
			TextReader textReader = new StreamReader(strFilename);
			string strValue = textReader.ReadToEnd();
			textReader.Close();
			return (T)JsonConvert.DeserializeObject<T>(strValue);
		}

    }
}
